﻿<?php

global $config;
if (isset($_GET['act'])) $act = $_GET['act'];

$list_url = "index.php?mod=contact&act=list";
$delete_url = $list_url . "&task=delete&id=";
$view_url = "index.php?mod=contact&act=view&id=";
?>
<?php

if ($act == "list") {
?>
    <h3 class='action-title'>Danh sách liên hệ</h3>
    <?php
    if (isset($_GET['task']) && $_GET['task'] == 'delete') {
        if (isset($_GET['id'])) $id = $_GET['id'];
        $sql = "delete from contact where id = $id";
        if ($db->query($sql)) notice("Đả xóa liên hệ.");
        else notice("Lổi: chưa cập nhật được dữ liệu.");
    }
    ?>
    <div id="list-posts">
        <div class="header-list">
            <label class="header-username">Họ tên</label>
            <label class="header-email">Email</label>
            <label class="header-level">Số điện thoại</label>
            <label class="header-date">Ngày gửi</label>
            <label class="header-edit">Xem</label>
            <label class="header-delete">Xóa</label>
            <div class="clear"></div>
        </div>
        <?php
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $limit = $config['news_per_page'];
            $start = $start = $limit * ($page - 1);
            $sql = "select * from contact order by id desc limit $start, $limit ";
            $data = $db->get_data($db->query($sql));
            
            if (!empty($data)) :
            foreach ($data as $contact) :
        ?>
        <div class="row-post">
            <label class="header-username"><?php echo $contact['fullname'] ?></label>
            <label class="header-email"><?php echo $contact['email'] ?></label>
            <label class="header-level"><?php echo $contact['phone'] ?></label>
            <label class="header-date"><?php echo $contact['created'] ?></label>
            <label class="header-edit"><a href="<?php echo $view_url.$contact['id'] ?>" class="edit-post" title="Sửa">Xem</a></label>
            <label class="header-delete"><a href="<?php echo $delete_url.$contact['id']; ?>" class="delete-post" title="Xóa">Xóa</a></label>
            <div class="clear"></div>
        </div>
        <?php
            endforeach;
            endif;
            ViewPages($config['news_per_page'], "contact", "contact");
        ?>
    </div>

<?php
} else if ($act == "view") {
    echo "<h3 class='action-title'>Xem liên hệ</h3>";
    $id = (isset($_GET['id'])) ? $_GET['id'] : 0;
	
	$sql = "select * from contact where id = $id";
    $data = $db->fetch_array($db->query($sql));
    if (!empty($data)) :
?>

	<p class="contact-row">
		<label class="contact-label">Họ tên</label>
		<input name="fullname" type="text" class="contact-input" value="<?php echo $data['fullname'] ?>" />
	</p>
	<p class="contact-row">
		<label class="contact-label">Địa chỉ</label>
		<input name="address" type="text" class="contact-input" value="<?php echo $data['address'] ?>" />
	</p>
	<p class="contact-row">
		<label class="contact-label">Email</label>
		<input name="email" type="text" class="contact-input" value="<?php echo $data['email'] ?>" />
	</p>
	<p class="contact-row">
		<label class="contact-label">Phone</label>
		<input name="phone" type="text" class="contact-input" value="<?php echo $data['phone'] ?>" />
	</p>
	<p class="contact-row">
		<label class="contact-label">Ngày gửi</label>
		<input name="phone" type="text" class="contact-input" value="<?php echo $data['created'] ?>" />
	</p>
	<p class="contact-row">
		<label class="contact-label">Nội dung</label>
		<textarea name="content" class="contact-content"><?php echo $data['content'] ?></textarea>
	</p>

<?php
	endif;
}
?>